import { useRef, useImperativeHandle } from 'react'
/* -------------------------------------------------------- Declare -- */
const { $lazy, $, $config } = window
/* -------------------------------------------------------- Async Component -- */
const SearchTable = $lazy.hook(()=>import('#cpt/temp/SearchTable'))
const isNative = $config.isNative
/* -------------------------------------------------------- Page Component -- */
const Component = ({ onChange, listRef, db, url }, ref) => {
	const tableRef = useRef()
	useImperativeHandle( ref, () => tableRef )
	const config = {
		auto:0,
		page : {
			title: { title:'集合', titleMode:2 },
			minWidth:null,
			contentClass: 'pl10'
		},
		// 表格配置
		table: {
			api 		: 'mongodb/collection/' + url + '/' + db,
			paging 		: 0,					// 接口是否分页
			isIndex 	: 1,
			idStr 		: 'name',
			setData 	: d => d.filter( v => !v.name.includes('chunks') ),
			row 		: { 
				type:1,
				onRowTrue: ({ name }) => {
					if( name.includes('.chunks') || name.includes('.files') ){
						name = name.split('.')[0]
						onChange(({ name, is_gridfs:1 }))
					}else{
						onChange({ name })
					}
				}
			},
			cols		: [
				{ title: '序号', 	dataIndex: 'index', width:60, align:'center' },
				{ title: '表', 			dataIndex: 'name'},
			],
			controls 	: {
				width: 70,
				data: ({ name }) => {
					let api = ''
					if( name.includes('.chunks') || name.includes('.files') ){
						api = 'mongodb/gridfs_bucket_del';
						name = name.split('.')[0]
					}
					return [
						!isNative && name !== 'fs' ? null : { mode:'delPop', label:'删除', r:1, abs:1, api, id:name, param:{db, url}, onOk:()=>$.ref(listRef).search() }
					]
				}
			},
		}
	}
	return <SearchTable ref={tableRef} {...config}/>
}
export default Component